#include "feature.h"
#include "def.h"

feature::feature() 
	: dictfile(def_all_feature_filename), spliter(new SimpleSpliter()), file_count(0) {
	}

feature::feature(ISpliter *p) : dictfile(def_all_feature_filename), spliter(p), file_count(0) {
}

feature::feature(std::string dictfile, ISpliter *p = new SimpleSpliter()) 
	: dictfile(dictfile), spliter(p), file_count(0) {
	}

	feature::~feature() {
		if(spliter)
			delete spliter;
	}

void feature::getFeatureOf(std::string target_file) {
	std::map<std::string, int> freq;
	std::map<std::string, int>::iterator itr;
	int count = 0;

	spliter->open(target_file);
	file_count++;

	while(spliter->hasNextWord()) {
		std::string word = spliter->nextWord();
		if(word.size() <= 0)
			continue;
		count++;

		itr = freq.find(word);
		if(itr != freq.end()) {
			itr->second ++;
		} else {
			freq.insert(std::pair<std::string, int>(word, 1));
		}

		updateDict(word, target_file);
	}
	spliter->close();

	std::string target_feature_file(target_file);
	target_feature_file += ".feature";
	std::ofstream output(target_feature_file.c_str());
	for(itr = freq.begin(); itr != freq.end(); itr++) {
		output << itr->first << "\t" << itr->second << "\t" << 
			static_cast<float>(itr->second) / count << std::endl;
	}
	output.close();
}

void feature::updateDict(std::string word, std::string target_file) {
	std::map<std::string, std::set<std::string> >::iterator dict_itr;
	dict_itr = dict.find(word);
	if(dict_itr != dict.end()) {
		dict_itr->second.insert(target_file);
	} else {
		std::set<std::string> filenames;
		filenames.insert(target_file);
		dict.insert(std::pair<std::string, std::set<std::string> >(word, filenames));
	}
}

void feature::writeToDictfile() {
	std::ofstream output(dictfile.c_str());
	std::map<std::string, std::set<std::string> >::iterator dict_itr;
	std::set<std::string>::iterator filename_itr;

	output << getFileCount() << std::endl;
	for(dict_itr = dict.begin(); dict_itr != dict.end(); dict_itr++) {
		//<word>	<file_count>	<filename1>,<filename2>,...,<filenamen>
		output << dict_itr->first << "\t" << dict_itr->second.size() << "\t";

		for(filename_itr = dict_itr->second.begin();
				filename_itr != dict_itr->second.end(); filename_itr++) {
			if(filename_itr != dict_itr->second.begin())
				output << ",";
			output << *filename_itr;
		}
		output << std::endl;
	}
	output.close();
}

int feature::getFileCount() {
	return file_count;
}
